//
//  SelectLangViewController.m
//  UEAssistant
//
//  Created by mac on 2017/7/18.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "SelectLangViewController.h"
#import "YZSortCell.h"

#define IsIos7 ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)

extern NSString * const YZUpdateMenuTitleNote;
static NSString * const IDS = @"cell";

@interface SelectLangViewController ()<UITableViewDelegate,UITableViewDataSource>{

    NSMutableArray          *_dataSource;
    NSMutableArray          *_dataSource_hot;
}

@property (nonatomic, strong) UITableView       *tableView;
@property (nonatomic, strong) UILabel           *naviLabel;
@property (nonatomic, strong) UIButton          *cancel;

@end

@implementation SelectLangViewController


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    // 这个页面被加载之后，状态栏设置为黑色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    // 页面即将消失时，状态栏再转为白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlizeDataSource];
    [self initlizeUserface];
}

- (void)initlizeDataSource{
    
    //读取json文件中的语言对
    NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];
    NSString *path=[mainBundleDirectory stringByAppendingPathComponent:@"alllanguage.json"];
    NSURL *url=[NSURL fileURLWithPath:path];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    NSLog(@"语言---%@",[self toArrayOrNSDictionary:data]);
    _dataSource = [NSMutableArray arrayWithArray:[self toArrayOrNSDictionary:data][@"result"]];
    
    _dataSource_hot = [NSMutableArray array];
    if (shareData.now_langauage_one) {
        [_dataSource_hot addObject:shareData.now_langauage_one];
    }
    if (shareData.now_langauage_two) {
        [_dataSource_hot addObject:shareData.now_langauage_two];
    }
    
}

- (void)initlizeUserface{

    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerClass:[YZSortCell class] forCellReuseIdentifier:IDS];
    [self.view addSubview:self.tableView];
    
    _naviLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, Screen_Width, 44)];
    _naviLabel.backgroundColor = [UIColor whiteColor];
    _naviLabel.textColor = RGB(55, 55, 55, 1);
    _naviLabel.text = @"语言列表";
    _naviLabel.font = [UIFont systemFontOfSize:16];
    _naviLabel.textAlignment = NSTextAlignmentCenter;
    [_naviLabel setUserInteractionEnabled:YES];
    [self.view addSubview:_naviLabel];
    
    _cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancel.frame = CGRectMake(15, 30, 20, 20);
    _cancel.backgroundColor = [UIColor clearColor];
    [_cancel setImage:[UIImage imageNamed:@"关闭xxhdpi"] forState:UIControlStateNormal];
    [_cancel addTarget:self action:@selector(cancelClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_cancel];
}


- (void)cancelClicked{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        
        return _dataSource_hot.count;
    }else{
    
        return _dataSource.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        if (_dataSource_hot.count == 0) {
            return 0.001f;
        }
        else{
        
            return 30*Radio;
        }
    }
    else{
        return 30*Radio;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        
        UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 30*Radio)];
        baseView.backgroundColor = RGB(247, 247, 247, 1);
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10*Radio, 10*Radio, 100, 20*Radio)];
        label.backgroundColor = [UIColor clearColor];
        label.text = @"最近使用";
        label.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
        label.textColor = RGB(161,161,161, 1);
        [baseView addSubview:label];
        return baseView;
        
    }else{
    
        UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 30*Radio)];
        baseView.backgroundColor = RGB(247, 247, 247, 1);
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10*Radio, 10*Radio, 100, 20*Radio)];
        label.backgroundColor = [UIColor clearColor];
        label.text = @"语言列表";
        label.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
        label.textColor = RGB(161,161,161, 1);
        [baseView addSubview:label];
        return baseView;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 40*Radio;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    YZSortCell *cell = [tableView dequeueReusableCellWithIdentifier:IDS];
    if (indexPath.section == 0) {
        cell.textLabel.text = _dataSource_hot[_dataSource_hot.count-1 - indexPath.row];
    }else{
        cell.textLabel.text = _dataSource[indexPath.row][@"dictionaryValue"];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
    cell.textLabel.textColor = RGB(55, 55, 55, 1);
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (self.didSelectSectionHandler != nil) {
        if (indexPath.section == 0) {
            self.didSelectSectionHandler(_dataSource_hot[_dataSource_hot.count-1 - indexPath.row]);
        }
        else{
            self.didSelectSectionHandler(_dataSource[indexPath.row][@"dictionaryValue"]);
        }
    }
    //把选中的语言存储用户偏好设置
    if (indexPath.section == 1) {
    
        if (shareData.now_langauage_one && !shareData.now_langauage_two) {
            shareData.now_langauage_two = _dataSource[indexPath.row][@"dictionaryValue"];
        }
        else if (shareData.now_langauage_one && shareData.now_langauage_two){
        
            shareData.now_langauage_one = _dataSource[indexPath.row][@"dictionaryValue"];
            NSString *temp = shareData.now_langauage_one;
            shareData.now_langauage_one = shareData.now_langauage_two;
            shareData.now_langauage_two = temp;
        }
        else{
        
            shareData.now_langauage_one = _dataSource[indexPath.row][@"dictionaryValue"];
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)setDidSelectDayHandler:(DidSelectSectionHandler)didSelectSectionHandler {
    
    _didSelectSectionHandler = didSelectSectionHandler;
    self.didSelectDayHandler = _didSelectSectionHandler; // 传递 block
    
}

- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:nil];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Screen_Width, Screen_Height-64) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = RGB(247, 247, 247, 1);
    }
    return _tableView;
}


@end
